// Fill the DB with example data on startup

import { Meteor } from 'meteor/meteor';

import { Roles } from '/imports/api/roles/roles.js';

import { createLink } from './fixlinks.js';



Meteor.startup(() => {

  // if (Meteor.roles.find({}).count() === 0) {
  //     // Define administrator role
  //     Meteor.roles.insert({
  //         name: "超级管理员",
  //         permissions: ['administrate']
  //     });
  //     // Define member role
  //     Meteor.roles.insert({
  //         name: "网站成员",
  //         permissions: ['vote', 'comment']
  //     });
  // }

  // let role = Meteor.roles.findOne({name: "超级管理员"});
  // let roleMember = Meteor.roles.findOne({name: "网站成员"});
  // Roles.addRolePerms('administrate', role._id);
  let admin = null;
  let superRole = null;
  if (Meteor.users.find({username: "admin"}).count() == 0) {
    //创建管理员账户
        let admin_id = Accounts.createUser({
            username: 'admin',
            password: 'yunchixing2017',
          });

        admin = Meteor.users.findOne({_id: admin_id});


  }else{
    admin = Meteor.users.findOne({username: "admin"});
  }

  if (Roles.find({name: "超级管理员"}).count() == 0) {
        Roles.insert({
          name: "超级管理员",
          weight: 0,
          createdAt: new Date(),
        });
        superRole = Roles.findOne({name: "超级管理员"});
  }else{
    superRole = Roles.findOne({name: "超级管理员"});
  }


  if (Roles.find({name: "网站成员"}).count() == 0) {
        Roles.insert({
          name: "网站成员",
          weight: 500,
          createdAt: new Date(),
        });
  }else{

  }

  if (Roles.find({name: "电商运营"}).count() == 0) {
        Roles.insert({
          name: "电商运营",
          weight: 250,
          createdAt: new Date(),
        });
  }else{

  }

  Meteor.users.update(admin._id, {
    $set: {
      role_ids: [superRole._id]
    }
  });



  createLink('链接管理', '/admin/links/1', '功能','1', 100);
  createLink('用户管理', '/admin/users/1', '功能','用户管理', 100);
  createLink('角色管理', '/admin/roles/1', '功能','用户管理', 100);
















});
